package com.qf.cloud2204.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

@Component
public class TestPostFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.POST_TYPE;  // 后置过滤器
    }

    @Override
    public int filterOrder() {
        return 3;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {


        // 获取该请求的起始时间戳
        RequestContext currentContext = RequestContext.getCurrentContext();
        long start = (long) currentContext.get("time-start");

        // 统计接口执行时长
        long end = System.currentTimeMillis();

        System.out.println("请求消耗的时间为:"+(end-start));

        return null;
    }
}
